package com.casic.ops.entity.vo;


import com.casic.ops.consts.app.BuildStatus;
import com.casic.ops.entity.domain.ApplicationBuildDO;
import com.casic.ops.utils.Utils;
import com.orion.utils.convert.TypeStore;
import com.orion.utils.time.Dates;
import lombok.Data;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @ClassName ops-platform
 * @Description  应用构建状态
 * @Author mianbin
 * @Date 2022/5/31 10:59
 * @Version 1.0
 **/
@Data
public class ApplicationBuildStatusVO {

    /**
     * id
     */
    private Long id;

    /**
     * 状态
     *
     * @see BuildStatus
     */
    private Integer status;

    /**
     * 构建开始时间
     */
    private Date startTime;

    /**
     * 构建开始时间
     */
    private String startTimeAgo;

    /**
     * 构建结束时间
     */
    private Date endTime;

    /**
     * 构建结束时间
     */
    private String endTimeAgo;

    /**
     * 使用时间 ms
     */
    private Long used;

    /**
     * 使用时间
     */
    private String keepTime;

    /**
     * action
     */
    private List<ApplicationActionStatusVO> actions;

    public static ApplicationBuildStatusVO buildDO2BuildStatusVO(ApplicationBuildDO p) {
        ApplicationBuildStatusVO vo = new ApplicationBuildStatusVO();
        vo.setId(p.getId());
        vo.setStatus(p.getBuildStatus());
        Date startTime = p.getBuildStartTime(), endTime = p.getBuildEndTime();
        vo.setStartTime(startTime);
        vo.setStartTimeAgo(Optional.ofNullable(startTime).map(Dates::ago).orElse(null));
        vo.setEndTime(endTime);
        vo.setEndTimeAgo(Optional.ofNullable(endTime).map(Dates::ago).orElse(null));
        if (startTime != null && endTime != null) {
            vo.setUsed(endTime.getTime() - startTime.getTime());
            vo.setKeepTime(Utils.interval(vo.getUsed()));
        }
        return vo;
    }

    static {
        TypeStore.STORE.register(ApplicationBuildDO.class, ApplicationBuildStatusVO.class, p -> {
            ApplicationBuildStatusVO vo = new ApplicationBuildStatusVO();
            vo.setId(p.getId());
            vo.setStatus(p.getBuildStatus());
            Date startTime = p.getBuildStartTime(), endTime = p.getBuildEndTime();
            vo.setStartTime(startTime);
            vo.setStartTimeAgo(Optional.ofNullable(startTime).map(Dates::ago).orElse(null));
            vo.setEndTime(endTime);
            vo.setEndTimeAgo(Optional.ofNullable(endTime).map(Dates::ago).orElse(null));
            if (startTime != null && endTime != null) {
                vo.setUsed(endTime.getTime() - startTime.getTime());
                vo.setKeepTime(Utils.interval(vo.getUsed()));
            }
            return vo;
        });
    }
}
